package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.entity.HotSongMenu;
import com.example.zbh.dsliao.ui.activty.HotSongMenuActivity;
import com.example.zbh.dsliao.utils.net.VolleySingleton;

import java.util.List;

/**
 * Created by yaolin on 15/11/21.
 */
public class RVHotSongMenuAdapter extends RecyclerView.Adapter<RVHotSongMenuAdapter.MyViewHolder> {

    private List<HotSongMenu> datas;
    private Context context;



    //网络请求
    VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public RVHotSongMenuAdapter(Context context) {
        this.context = context;
        imageLoader = volleySingleton.getImageLoader();
    }


    public void addData(List<HotSongMenu> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.list_hot_song_menu, null);
        return  new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(datas != null && datas.size() > 0){
            HotSongMenu h = datas.get(position);
            holder.song.setText(h.getTitle());
            if(h.getPic_url() != null);
            String imgUrl = h.getPic_url();
            getBitmap(imgUrl, holder.img);
        }

        holder.position = position;
    }

    private void getBitmap(String url, NetworkImageView imageView) {
        imageView.setDefaultImageResId(R.mipmap.music_cache_icon);
        imageView.setErrorImageResId(R.mipmap.music_cache_icon);
        imageView.setImageUrl(url , imageLoader);
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? 6 : 0;
    }



    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView img;
        private TextView song;
        private RelativeLayout relativeLayout;
        private int position;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (NetworkImageView) itemView.findViewById(R.id.img);
            song = (TextView) itemView.findViewById(R.id.song);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
            relativeLayout.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if(datas != null && datas.size() > 0){
                HotSongMenu h = datas.get(position);
                Intent intent = new Intent(context, HotSongMenuActivity.class);
                intent.putExtra("id", h.get_id());
                intent.putExtra("title", h.getTitle());
                intent.putExtra("pic", h.getPic_url());
                context.startActivity(intent);
            }
        }
    }
}
